package com.gable.exhibition.controller;

import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.init.ManufacturerInit;
import com.gable.exhibition.init.SupplierManufacturerInit;
import com.gable.exhibition.model.Manufacturer;
import com.gable.exhibition.service.ContractEstimateSendService;
import com.gable.exhibition.util.ExceptionUtil;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mj on 2018/2/6.
 * 合同预计发货
 */
@Controller
@RequestMapping(value = "/contractEstimateSend")
public class ContractEstimateSendController {

    @Autowired
    ContractEstimateSendService contractEstimateSendServer;

    //预计发货提醒
    @RequestMapping(value="/remind",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil remind(HttpServletRequest request, ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = parameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);

            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            Long hospitalId = sessionUser.getHospitalId();
            Long supplierId = sessionUser.getSupplierId();
            if(manufacturerId == null && hospitalId == null && supplierId == null)
                return new ReturnResultUtil(500,MessageUtil.ILLEGALUSER);
            parameterView.setManufacturerId(manufacturerId);
            parameterView.setHospitalId(hospitalId);
            parameterView.setSupplierId(supplierId);

            resultUtil = contractEstimateSendServer.querySendRemind(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //待发货列表
    @RequestMapping(value="/waitSend",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil waitSend(HttpServletRequest request,ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = parameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);

            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);

            Long manufacturerId = sessionUser.getManufacturerId();  //厂商ID
            Long hospitalId = sessionUser.getHospitalId();          //医院ID
            Long supplierId = sessionUser.getSupplierId();          //供应商ID
            if(manufacturerId == null && hospitalId == null && supplierId == null)
                return new ReturnResultUtil(500,MessageUtil.ILLEGALUSER);

            parameterView.setHospitalId(hospitalId);
            parameterView.setManufacturerId(manufacturerId);
            parameterView.setSupplierId(supplierId);

            resultUtil = contractEstimateSendServer.waitSend(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //待发货详情
    @RequestMapping(value="/waitSendInfo",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil waitSendInfo(HttpServletRequest request,ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long contractEstimateSendId = parameterView.getContractEstimateSendId();
            if(contractEstimateSendId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTESTIMATESENDID);
            resultUtil = contractEstimateSendServer.waitSendInfo(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //预计发货，发货
    @RequestMapping(value="/sendForEstimate",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil sendForEstimate(HttpServletRequest request, SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.FAILED);
        try {
            Long userId = sendParameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);

            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            String manufacturerName = sessionUser.getManufacturerName();
            Long supplierId = sessionUser.getSupplierId();
            String supplierName = sessionUser.getSupplierName();

            sendParameterView.setManufacturerId(manufacturerId);
            sendParameterView.setManufacturerName(manufacturerName);
            sendParameterView.setSupplierId(supplierId);
            sendParameterView.setSupplierName(supplierName);

            Long contractId = sendParameterView.getContractId();
            Long contractEstimateSendId = sendParameterView.getContractEstimateSendId();
            String sendDate = sendParameterView.getSendDate();
            //[{contractDeviceId:1,actualQuantity:100,surplusQuantity:50,remark:xxx}]
            String sendDeviceJson = sendParameterView.getSendDeviceJson();
            String sendNo = sendParameterView.getSendNo();
            String projectChargePerson = sendParameterView.getProjectChargePerson();//工程师名称
            String projectChargePersonPhone = sendParameterView.getProjectChargePersonPhone();//工程师电话
            String estimateInstallDate = sendParameterView.getEstimateInstallDate();//预计安装时间
            String hospitalContacts = sendParameterView.getHospitalContacts();
            String hospitalPhone = sendParameterView.getHospitalPhone();
            Integer oneself = sendParameterView.getOneself();
            Long serviceType = sendParameterView.getServiceType();
            Long serviceId = sendParameterView.getServiceId();

            if(contractId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTIDNOEXIST);
            if(contractEstimateSendId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTESTIMATESENDID);
            if(StringUtil.isEmpty(sendDate))
                return new ReturnResultUtil(500,MessageUtil.SENDDATENOEXIST);
            if(StringUtil.isEmpty(sendDeviceJson))
                return new ReturnResultUtil(500,MessageUtil.SENDDEVICEJSON);
            if(StringUtil.isEmpty(sendNo))
                return new ReturnResultUtil(500,MessageUtil.SENDNONOTNULL);
            if(StringUtil.isEmpty(hospitalContacts))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALCONTACTS);
            if(StringUtil.isEmpty(hospitalPhone))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALPHONE);
            if(oneself == null)
                return new ReturnResultUtil(500,MessageUtil.ONESELFISNULL);
            if(serviceType == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICETYPE);
//            if(oneself == 1){
//                if(StringUtil.isEmpty(projectChargePerson))
//                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSON);
//                if(StringUtil.isEmpty(projectChargePersonPhone))
//                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSONPHONE);
//                if(StringUtil.isEmpty(estimateInstallDate))
//                    return new ReturnResultUtil(500,MessageUtil.ESTIMATEINSTALLDATE);
//            }
            if(oneself == 0 && manufacturerId != null && serviceId == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICEID);
            resultUtil = contractEstimateSendServer.sendForEstimate(sendParameterView);
        }catch (ExceptionUtil eu){
            return new ReturnResultUtil(eu.getRet(),eu.getMsg(),eu.getData());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }
}